using System;
using System.Collections;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using GriffinNav.GPS;
using GriffinNav.Map;
using GriffinNav.UI.Logging;

namespace GriffinNav.UI
{
    public static class Global
    {
        public static bool Debug = true;
        public static string[] SerialPorts = null;
        public static GpsData Gps = null;
        public static GriffinNav.Map.Map Map = null;

        private static frmOptions mOptions = null;
        private static frmViewer mMapViewer = null;
        private static frmGpsData mGpsInfo = null;
        private static frmAbout mAbout = null;

        private static string mAppName = "Griffin Navigator";
        private static string mAppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

        [MTAThread]
        static void Main()
        {
            // get names of available serial ports
            SerialPorts = SerialPort.GetPortNames();
            Array.Sort(SerialPorts);

            Application.Run(new frmViewer());

            try
            {
                // save application settings and state information
                AppSettings.Save();
                //AppState.Save();
            }
            catch (Exception ex)
            {
                Global.ShowError("Error", ex.Message);
            }
        }

        public static void Gps_OnGpsError(GPSException ex, bool showMessage)
        {
            if (showMessage)
            {
                Global.ShowError("Error", ex.Message);
            }
        }

        #region Application Information
        public static string Name
        {
            get
            {
                return mAppName;
            }
        }


        public static string AppPath
        {
            get
            {
                return mAppPath;
            }
        }
        #endregion

        #region Form Accessors
        public static frmOptions Options
        {
            get
            {
                if (mOptions == null)
                {
                    mOptions = new frmOptions();
                }

                return mOptions;
            }
        }

        public static frmViewer MapViewer
        {
            get
            {
                if (mMapViewer == null)
                {
                    mMapViewer = new frmViewer();
                }

                return mMapViewer;
            }
        }

        public static frmGpsData GpsInfo
        {
            get
            {
                if (mGpsInfo == null)
                {
                    mGpsInfo = new frmGpsData();
                }

                return mGpsInfo;
            }
        }

        public static frmAbout About
        {
            get
            {
                if (mAbout == null)
                {
                    mAbout = new frmAbout();
                }

                return mAbout;
            }
        }
        #endregion

        public static void ShowError(string title, string message)
        {
            MessageBox.Show(message, title,
                MessageBoxButtons.OK,
                MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button1);
        }

        public static bool InitGps(bool forceInit)
        {
            try
            {
                if (!forceInit && Gps != null)
                {
                    return true; // GPS initialized
                }

                if (!string.IsNullOrEmpty(AppSettings.GpsPort) && AppSettings.GpsBaudRate > 0)
                {
                    if (forceInit || Gps == null)
                    {
                        // initialize GPS with current application settings
                        Gps = new GpsData(AppSettings.GpsPort, AppSettings.GpsBaudRate);

                        return true;
                    }
                }
                else if (string.IsNullOrEmpty(AppSettings.GpsPort) || AppSettings.GpsBaudRate <= 0)
                {
                    // intialize GPS with no port or baud rate settings
                    Gps = new GpsData();

                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        internal static void LogError(string assemblyName, Exception ex)
        {
            try
            {
                ErrorLog log = new ErrorLog();

                log.WriteError(assemblyName, ex);

                log = null;
            }
            finally { }
        }

        internal static void LogError(string assemblyName, UIException ex)
        {
            try
            {
                ErrorLog log = new ErrorLog();

                log.WriteError(assemblyName, ex);

                log = null;
            }
            finally { }
        }

        internal static void LogError(string assemblyName, GPSException ex)
        {
            try
            {
                ErrorLog log = new ErrorLog();

                log.WriteError(assemblyName, ex);

                log = null;
            }
            finally { }
        }

        internal static void LogError(string assemblyName, MapException ex)
        {
            try
            {
                ErrorLog log = new ErrorLog();

                log.WriteError(assemblyName, ex);

                log = null;
            }
            finally { }
        }
    }
}